package com.sky.service.impl;


import ch.qos.logback.core.status.StatusUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.StemealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.sky.constant.MessageConstant.SETMEAL_ENABLE_FAILED;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    StemealDishMapper stemealDishMapper;

    @Autowired
    DishMapper dishMapper;

    /**
     * 套餐分页
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> list = setmealMapper.page(setmealPageQueryDTO);

        PageInfo<Setmeal> pagePageInfo = new PageInfo<>(list);

        List<SetmealVO> voList = list.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);

            Category category = categoryMapper.findById(setmeal.getCategoryId());
            setmealVO.setCategoryName(category.getName());
            return setmealVO;
        }).collect(Collectors.toList());
        return new PageResult(pagePageInfo.getTotal(), voList);

    }


    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.add(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setDishId(setmeal.getId());
            setmealDish.setSetmealId(setmeal.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        stemealDishMapper.saveDish(setmealDishes);

    }


    /**
     * 根据id查找套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO findById(Long id) {
        Setmeal setmeal = setmealMapper.findById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> list = stemealDishMapper.findByDishId(setmeal.getId());
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 停售或启售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrEnd(Integer status, Long id) {
        //如果是启售就进行业务判断
        if (status == StatusConstant.ENABLE) {
            List<SetmealDish> list = stemealDishMapper.findByDishId(id);
            list.forEach(setmealDish -> {
                //根据dish_id查询菜品
                Dish dish = dishMapper.findById(setmealDish.getDishId());
                //如果菜品中有未启售，直接抛出异常
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(SETMEAL_ENABLE_FAILED);
                }
            });
        }

        //如果没有上述情况，就可以更改
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);


        setmealMapper.update(setmeal);
    }

    /**
     * 删除一条或者多条
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delByids(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        ids.forEach(id -> {
            stemealDishMapper.delByDishId(id);
        });

        setmealMapper.delByids(ids);
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public Result update(SetmealDTO setmealDTO) {
        ArrayList<Long> list = new ArrayList<>();
        list.add(setmealDTO.getId());
        for (Long aLong : list) {
            stemealDishMapper.delByDishId(aLong);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        if (setmealDTO.getSetmealDishes().size() == 0) {
            return Result.success();
        }

        List<SetmealDish> setmealDishes = setmealDTO
                .getSetmealDishes().stream()
                .map(setmealDish -> {
                    setmealDish.setSetmealId(setmeal.getId());
                    return setmealDish;
                }).collect(Collectors.toList());

        stemealDishMapper.saveDish(setmealDishes);
        return Result.success();
    }

    /**
     * 条件查询
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        List<Setmeal> list = setmealMapper.list(setmealDTO);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        //根据套餐id查询绑定的所有菜品关系
        List<SetmealDish> setmealDishes = stemealDishMapper.findBySetmealId(id);
        List<DishItemVO> dishItemList = setmealDishes.stream()
                .map(setmealDish -> {
                    Long dishId = setmealDish.getDishId();
                    //根据菜品id查询菜品信息
                    Dish dish = dishMapper.findById(dishId);
                    //封装DishItem
                    DishItemVO dishItemVO = new DishItemVO();
                    dishItemVO.setImage(dish.getImage());
                    dishItemVO.setDescription(dish.getDescription());
                    dishItemVO.setCopies(setmealDish.getCopies());
                    dishItemVO.setName(setmealDish.getName());
                    return dishItemVO;
                }).collect(Collectors.toList());

        return dishItemList;
    }
}
